package cn.edu.scau.cmi.yanweijie.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import cn.edu.scau.cmi.yanweijie.jdbc.Instructor;
import cn.edu.scau.cmi.yanweijie.jdbc.InstructorMapper;
import cn.edu.scau.cmi.yanweijie.jdbc.SqlSessionFactoryUtils;
import cn.edu.scau.cmi.yanweijie.jdbc.Student;
import cn.edu.scau.cmi.yanweijie.jdbc.StudentMapper;
import jdk.internal.org.objectweb.asm.tree.IntInsnNode;

public class JdbcClient {

	public SqlSession session = null;
	public InstructorMapper instructorMapper = null;
	public StudentMapper studentMapper = null;
	
	public JdbcClient() {
		init();
	}
	public void init() {
		this.session = SqlSessionFactoryUtils.openSqlSession();
		this.instructorMapper = session.getMapper(InstructorMapper.class);
		this.studentMapper = session.getMapper(StudentMapper.class);
	}
	
	public static void main(String[] args) throws InterruptedException {
		JdbcClient jdbcClient = new JdbcClient();
		SqlSession sqlSession = jdbcClient.session;
		StudentMapper studentMapper = jdbcClient.studentMapper;
		if (sqlSession==null) {
			System.out.println("what1");
			
		}
		System.out.println("功能0：查看列表\r\n" + 
				"功能1：添加学生\r\n" + 
				"功能2：修改学生\r\n" + 
				"功能3：删除学生\r\n" + 
				"功能4：添加教师\r\n" + 
				"功能5：修改教师\r\n" + 
				"功能6：删除教师\r\n" + 
				"功能7：学生选择指导教师\r\n" + 
				"功能8：学生修改指导教师\r\n" + 
				"功能9：教师选择被指导的学生\r\n" + 
				"功能10：教师修改被指导的学生\r\n" + 
				"功能11：获取所有学生列表\r\n" + 
				"功能12：获取所有老师列表\r\n" + 
				"");
		Scanner scn = new Scanner(System.in);
		while (true) {
			int type = scn.nextInt();
			switch (type) {
			case 0: System.out.println("功能0：查看列表\r\n" + 
					"功能1：添加学生\r\n" + 
					"功能2：修改学生\r\n" + 
					"功能3：删除学生\r\n" + 
					"功能4：添加教师\r\n" + 
					"功能5：修改教师\r\n" + 
					"功能6：删除教师\r\n" + 
					"功能7：学生选择指导教师\r\n" + 
					"功能8：学生修改指导教师\r\n" + 
					"功能9：教师选择被指导的学生\r\n" + 
					"功能10：教师修改被指导的学生\r\n" + 
					"功能11：获取所有学生列表\r\n" + 
					"功能12：获取所有老师列表\r\n" + 
					"");break;
			case 1: jdbcClient.addStudent(sqlSession);break;
			case 2: jdbcClient.updateStudent(sqlSession);break;
			case 3: jdbcClient.deleteStudent(sqlSession);break;
			case 4: jdbcClient.addInstructor(sqlSession);break;
			case 5: jdbcClient.updateInstructor(sqlSession); break;
			case 6: jdbcClient.deleteInstructor(sqlSession);break;
			case 7: jdbcClient.stuPickIns(sqlSession);break;
			case 8: jdbcClient.stuChangeIns(sqlSession);break;
			case 9: jdbcClient.insPickStu(sqlSession);break;
			case 10: jdbcClient.insChangeStu(sqlSession);break;
			case 11: jdbcClient.checkAllStudent(sqlSession);break;
			case 12: jdbcClient.checkAllInstrucotr(sqlSession);break;
			
			default:
				break;
			}
			
		}
	}
	public void checkAllStudent(SqlSession session) {
		List<Student> students = new ArrayList<Student>();
		students = studentMapper.getAllStudent();
		for(int i=0;i<students.size();i++) {
			System.out.println("student["+i+"]"+students.get(i).toString());
		}
	}
	public void checkAllInstrucotr(SqlSession session) {
		List<Instructor> instructors = new ArrayList<Instructor>();
		instructors = instructorMapper.getAllInstructors();
		for(int i=0;i<instructors.size();i++) {
			System.out.println("instructor["+i+"]"+instructors.get(i).toString());
		}
	}
	
	public int addStudent(SqlSession session) {
		Student student = new Student();
		Scanner scanner  = new Scanner(System.in);
		System.out.println("添加学生中");
		System.out.print("请输入名字：");

		student.setStuName((scanner.next()));
		
		System.out.print("请输入学号：");
		student.setStuId(Integer.valueOf(scanner.next()));
		int result = studentMapper.insertOnlyStudent(student);
		session.commit();
		return result;
	}
	
	public void updateStudent(SqlSession session) {
		Scanner scanner  = new Scanner(System.in);
		System.out.print("修改学生姓名,输入你要修改学生的id号：");
		int id = scanner.nextInt();
		int result=0;
		System.out.print("输入你希望修改出来的名称：");
		String nameString = scanner.next();
		Student student = new Student(nameString,id);
		result +=studentMapper.updateStudent(student);
		if (result==1) {
			System.out.println("更新成功");
			session.commit();
		}else {
			System.out.println("更新失败");
		}
	}
	
	public void deleteStudent(SqlSession session) {
		Scanner scanner  = new Scanner(System.in);
		System.out.print("输入你要删除的学生的id号：");
		int result=0;
		result +=studentMapper.deleteStudent(scanner.nextInt());
		if (result==1) {
			System.out.println("删除成功");
			session.commit();
		}else {
			System.out.println("删除失败");
		}
	}
	
	public void addInstructor(SqlSession session) {
		Instructor instructor = new Instructor();
		Scanner scanner  = new Scanner(System.in);
		System.out.println("添加老师中");
		System.out.print("请输入名字：");

		instructor.setInstructorName((scanner.next()));
		
		System.out.print("请输入工号：");
		instructor.setInstructorId(Integer.valueOf(scanner.next()));
		int result = instructorMapper.insertOnlyInstructor(instructor);
		if (result==1) {
			System.out.println("插入成功");
			session.commit();
		}else {
			System.out.println("插入失败");
		}
	}
	
	public void updateInstructor(SqlSession session) {
		Scanner scanner  = new Scanner(System.in);
		System.out.print("修改老师姓名,输入你要修改老师的id号：");
		int id = scanner.nextInt();
		int result=0;
		System.out.print("输入你希望修改出来的名称：");
		String nameString = scanner.next();
		Instructor instructor = new Instructor(nameString,id);
		result +=instructorMapper.updateInstructor(instructor);
	}
	
	public void deleteInstructor(SqlSession session) {
		Scanner scanner  = new Scanner(System.in);
		System.out.print("输入你要删除的老师的id号：");
		int result=0;
		result +=instructorMapper.deleteInstructor(scanner.nextInt());
		if (result==1) {
			System.out.println("删除成功");
			session.commit();
		}else {
			System.out.println("删除失败");
		}
	}
	
	public void stuPickIns(SqlSession session) {
		Scanner scanner  = new Scanner(System.in);
		System.out.print("请输入学生id:");
		int id = scanner.nextInt();
		System.out.print("请输入老师id:");
		int id2 = scanner.nextInt();
		int result =0;
		Student student = studentMapper.getStudent(id);
		student.setInstructorId(id2);
		result+= studentMapper.updateStudentInstructor(student);
		if (result==1) {
			System.out.println("选择成功");
			session.commit();
		}else {
			System.out.println("选择失败");
		}
	}
	public void stuChangeIns(SqlSession session) {
		Scanner scanner  = new Scanner(System.in);
		System.out.print("请输入学生id:");
		int id = scanner.nextInt();
		System.out.print("请输入修改后的老师id:");
		int id2 = scanner.nextInt();
		int result =0;
		Student student = studentMapper.getStudent(id);
		System.out.println("这个是修改前的学生信息："+student.toString());
		student.setInstructorId(id2);
		result+= studentMapper.updateStudentInstructor(student);
		System.out.println("这个是修改后的学生信息："+student.toString());
		if (result==1) {
			System.out.println("更改成功");
			session.commit();
		}else {
			System.out.println("更改失败");
		}
	}
	public void insPickStu(SqlSession session) {
		stuPickIns(session);
	}

	public void insChangeStu(SqlSession session) {
		stuChangeIns(session);
	}
}
